package capstoneproject.dudiachi.Main;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceCategory;
import android.preference.PreferenceFragment;

public class SettingFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener {

	public static String CO_CHU = "pref_cochu";
	public static String THOI_GIAN_CHU_THICH = "pref_chuthich";
	public static String TRUY_CAP_LE_HOI = "pref_lehoi";
	public static String TRUY_CAP_DIA_DANH = "pref_diadanh";
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		addPreferencesFromResource(R.xml.pref);
		
		for(int i = 0; i < getPreferenceScreen().getPreferenceCount(); i++){
			initSummary(getPreferenceScreen().getPreference(i));
		}
	}
	
	@Override
	public void onResume() {
		super.onResume();
		getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
	}
	
	@Override
	public void onPause() {
		super.onPause();
		getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
	}
	
	public void initSummary(Preference p){
		if(p instanceof PreferenceCategory){
			PreferenceCategory pCat = (PreferenceCategory) p;
			for(int i = 0; i < pCat.getPreferenceCount(); i++){
				initSummary(pCat.getPreference(i));
			}
			
		}else{
			updatePrefSummary(p);
		}
	}

	public void updatePrefSummary(Preference p){
		if(p instanceof ListPreference){
			ListPreference listPref = (ListPreference) p;
			p.setSummary(listPref.getEntry());
		}
	}
	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		updatePrefSummary(findPreference(key));
		
	}

}
